<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/22
 * Time: 10:30
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;

class Gifts extends Api
{

    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['*'];

    //送礼页面
    public function giveNow(){
        $goods_id = (int)input('post.goods_id');
        $user_id =  (int)input('post.user_id');
        $number   = (int)input('post.number');
        $spec     = (int)input('post.spec');
        if (!$goods_id){
            $this->error('参数goods_id不能为空');
        }
        if (!$user_id){
            $this->error('参数user_id不能为空');
        }
        if (!$number){
            $this->error('参数number不能为空');
        }
        $data['goods']   = Db::name('goods_spu')->where('id',$goods_id)->find();
        $data['goods']['image'] = $this->url.$data['goods']['image'];
        if ($spec){
            $data['spec'] = Db::name('goods_spec')->where('id',$spec)->find();
        }else{
            $data['spec'] = null;
        }
        $data['number'] = $number;
        $data['total'] = $data['goods']['price'] * $number;
        if (!$data){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }
    //送礼提交
    public function giveSub(){
        $user_id     = (int)input('post.user_id');
        $goods_id    = (int)input('post.goods_id');
        $number      = (int)input('post.number');
        $spec        = (int)input('post.spec_id');
        if (!$goods_id){
            $this->error('参数goods_id不能为空');
        }
        if (!$user_id){
            $this->error('参数user_id不能为空');
        }
        if (!$number){
            $this->error('参数number不能为空');
        }
        $goods = Db::name('goods_spu')->where('id',$goods_id)->find();
        $price = $goods['price'];
        if ($spec){
            $ret = Db::name('goods_spec')->where($spec)->find();
            $price = $ret['price'];
            if ($ret['stock'] < $number){
                $this->error('库存不足');
            }
        }
        if ($goods['stock'] < $number){
            $this->error('库存不足');
        }
        $orderMoney = $price * $number;
        $payMoney   = $orderMoney;
        if ($goods['ship'] == 1){
            $payMoney = $payMoney - $goods['ship_money'];
        }
        $order_id = date('Ymdhis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        $data = [
            'give_id' =>$user_id,
            'goods_id'=>$goods_id,
            'spec_id'    =>0,
            'order_id'=>$order_id,
            'number' =>$number,
            'money'   =>$payMoney,
            'create_time'=>time(),
            'status'=>0,
        ];
        if ($spec){
            $data['spec_id'] = $spec;
        }
        $insert = Db::name('member_gifts')->insert($data);
        $num = Order::goodsNumber($data['goods_id'],$data['number'],$spec,0);
        if (!$insert && !$num){
            $this->error('请求失败');
        }
        $this->success('请求成功',$data);
    }
    //收礼页面
    public function collectInfo(){
        $user_id = input('get.user_id');
        $gift_id = input('get.gift_id');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        if (!$gift_id){
            $this->error('gift_id不能为空');
        }
        $data['gift'] = Db::name('member_gifts')->where('user_id',$user_id)->where('id',$gift_id)->find();
        $data['goods']= Db::name('goods_spu')->where('id',$data['gift']['goods_id'])->find();
        $data['goods']['image'] = $this->url.$data['goods']['image'];
        if (!$data['gift']['spec_id']){
            $data['spec'] = Db::name('goods_spec')->where('id',$data['gift']['spec_id'])->find();
        }else{
            $data['spec'] =null;
        }
        $data['user'] = Db::name('member_user')->where('id',$data['gift']['give_id'])->find();
        if (!empty($data)){
            $this->error('无数据');
        }
        $this->success('请求成功');
    }
    //收礼
    public function collectSub(){
        $give_id = input('post.give_id');
        $user_id = input('post.user_id');
        $gift_id = input('post.gift_id');
        $addr_id = input('post.addr_id');
        if (!$gift_id && !$user_id && !$give_id){
            $this->error('参数不能为空');
        }
        $gift = Db::name('member_gift')->where('id',$gift_id)->find();
        $goods = Db::name('goods_spu')->where('id',$gift['goods_id'])->find();
        $data = [
            'order_id'=>$gift['order_id'],
            'user_id' =>$user_id,
            'money'   =>$gift['money'],
            'pay_money'=>$gift['money'],
            'status'=>1,
            'addr_id'=>$addr_id,
            'time'=>time(),
            'collage'=>0,
            'remark' =>''
        ];
        $order = Db::name('order_sku')->insert($data);
        $goods_data = [
            'order_id'=>$gift['order_id'],
            'goods_id'=>$goods['id'],
            'price'   =>$gift['money'],
            'spec_id' =>$gift['spec_id'],
            'number'  =>$gift['number']
        ];
        $order_goods = Db::name('order_goods')->insert($goods_data);




    }



    private function template(){

        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$this->access_token;
//        $ret = Db::name()->find();
//        $data = [
//            'touser'=>,
//        ];
//        $this->curlRequest($url,$data);
    }







}